<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, UserConfig, Config, Team, MachineLog, Levelup};
use think\Db;
use app\facade\Redis;

class Levels extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Levels')->setDescription('用户升级机制');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/rskj/think Levels'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        //获取id
        $id = Redis::rPop('lists');
        if (!empty($id) && $id > 0) {
            //获取用户上级结构
            $structure = (new Users())->where('id', $id)->value('structure');
            $people = explode(',', $structure);
            $people = array_filter($people);
            if (count($people) > 0) {
                //获取团队累计代数
                $MachineRewards = (new Config())->where('key', 'machine_rewards')->value('value');
                $MachineRewards = empty($MachineRewards) ? 10 : (int)$MachineRewards;
                $tree = [];
                if ($MachineRewards < 1) {
                    $MachineRewards = 1;
                }
                for ($i = 1; $i <= $MachineRewards; $i++) {
                    $tree[] = $i;
                }
                $users = (new Users())->where(['id' => $people, 'level' => [1, 2]])->select();
                Db::startTrans();
                try {
                    foreach ($users as $user) {
                        $ones = 0; //直推人数
                        $machines = 0; //团队总矿机数
                        $team = []; //团队人员
                        //获取团队所有人
                        $myteam = (new Team())->field('supe_id,algebra')->where(['user_id' => $user['id'], 'algebra' => $tree])->select();
                        //整个团队信息
                        foreach ($myteam as $teams) {
                            if ($teams['algebra'] == 1) {
                                $ones += 1;
                            }
                            //团队用户ID
                            $team[] = $teams['supe_id'];
                        }
                        if (count($team) > 0) {
                            $machines = (new MachineLog())->where(['user_id' => $team])->count();
                        }
                        if ($user['level'] <= 2) {
                            $userconfig = (new UserConfig())->where('level', 3)->find();
                            if (!empty($userconfig) && $ones >= $userconfig['direpush'] && $machines >= $userconfig['buypeople']) {
                                (new Users())->where(['id' => $user['id']])->update(['level' => 3]);
                                (new Levelup())->insert([
                                    'user_id' =>  $user['id'],
                                    'type' => 1,
                                    'levelbf' => $user['level'],
                                    'levelaf' => 3
                                ]);
                            } else {
                                if ($user['level'] <= 1) {
                                    $userconfig = (new UserConfig())->where('level', 2)->find();
                                    if (!empty($userconfig) && $ones >= $userconfig['direpush'] && $machines >= $userconfig['buypeople']) {
                                        (new Users())->where(['id' => $user['id']])->update(['level' => 2]);
                                        (new Levelup())->insert([
                                            'user_id' =>  $user['id'],
                                            'type' => 1,
                                            'levelbf' => $user['level'],
                                            'levelaf' => 2
                                        ]);
                                    }
                                }
                            }
                        }
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Redis::lPush('lists', $id);
                    Db::rollback();
                }
                echo '成功！！';
            }
        }
    }
}
